package com.lonely.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lonely.common.annotation.Log;
import com.lonely.common.core.domain.Select;
import com.lonely.common.core.domain.api.R;
import com.lonely.common.enums.BusinessType;
import com.lonely.common.utils.SecurityUtils;
import com.lonely.system.utils.AuthUtil;
import com.lonely.system.domain.dto.role.*;
import com.lonely.system.domain.vo.system.role.QueryToBeAllocatedUserInfoVo;
import com.lonely.system.domain.vo.system.role.SysRoleInfoVo;
import com.lonely.system.domain.vo.system.role.SysRolePageQueryVo;
import com.lonely.system.domain.vo.system.role.SysRoleUserInfoVo;
import com.lonely.system.service.ISysRoleService;
import com.lonely.system.service.ISysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 *
 * @author fzzf
 */
@RestController
@RequestMapping("/systemManage/role")
@Api(tags = "系统管理/角色管理")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserRoleService userRoleService;

    /**
     * 角色分页查询列表
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "角色分页查询列表")
//    @SaCheckPermission("system:role:page")
    public R<Page<SysRolePageQueryVo>> page(@RequestBody SysRolePageQueryDto queryDto) {
        return R.data(this.roleService.page(queryDto));
    }

    /**
     * 根据角色id获取角色信息
     *
     * @param roleId
     * @return
     */
    @GetMapping("/getRoleInfo/{roleId}")
    @ApiOperation(value = "根据角色id查询角色信息")
    public R<SysRoleInfoVo> getRoleInfo(@PathVariable("roleId") Long roleId) {
        return R.data(this.roleService.getRoleInfo(roleId));
    }


    /**
     * 创建角色
     *
     * @param submitDto
     * @return
     */
    @SaCheckPermission("system:role:create")
    @PostMapping("/create")
    @ApiOperation(value = "创建角色")
    public R<Boolean> create(@RequestBody SysRoleSubmitDto submitDto) {
        this.roleService.create(submitDto);
        return R.status(true);
    }

    /**
     * 编辑角色
     *
     * @param submitDto
     * @return
     */
    @SaCheckPermission("system:role:edit")
    @PostMapping("/edit")
    @ApiOperation(value = "编辑角色")
    public R<Boolean> edit(@RequestBody SysRoleSubmitDto submitDto) {
        this.roleService.edit(submitDto);

        // 刷新登录用户的权限
        AuthUtil.refreshUserPermission(SecurityUtils.getUserId());
        return R.status(true);
    }

    /**
     * 删除角色
     */
    @SaCheckPermission("system:role:delete")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{roleId}")
    public R<Boolean> delete(@PathVariable("roleId") Long roleId) {
        this.roleService.deleteRoleById(roleId);

        // 刷新登录用户的权限
        AuthUtil.refreshUserPermission(SecurityUtils.getUserId());
        return R.status(true);
    }

    /**
     * 批量删除角色
     */
    @SaCheckPermission("system:role:batchDelete")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("/batchDelete")
    public R<Boolean> batchDelete(@RequestBody BatchDeleteRoleDto requestDto) {
        this.roleService.batchDeleteByRoleIds(requestDto);
        return R.status(true);
    }


    /**
     * 新增角色分页查询列表
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/addRolePage")
    @ApiOperation(value = "新增角色分页查询列表")
    public R<Page<SysRolePageQueryVo>> addRolePage(@RequestBody @Validated AddRolePageQueryDto queryDto) {
        return R.data(this.roleService.addRolePage(queryDto));
    }


    /**
     * 获取角色下拉列表选项值
     *
     * @return
     */
    @GetMapping("/getRoleSelectOptions")
    @ApiOperation("获取角色下拉列表选项值")
    public R<List<Select>> getRoleSelectOptions() {
        return R.data(this.roleService.getRoleSelectOptions());
    }


    /**
     * 获取角色分配的用户信息分页列表
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/getRoleUserInfoPage")
    @ApiOperation("获取角色分配的用户信息分页列表")
    public R<Page<SysRoleUserInfoVo>> getRoleUserInfoPage(@RequestBody @Validated SysRoleUserPageQueryDto queryDto) {
        return R.data(this.roleService.getRoleUserInfoPage(queryDto));
    }


    /**
     * 获取指定角色待分配的用户信息分页列表
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/getRoleToBeAllocatedUserInfoPage")
    @ApiOperation("获取指定角色待分配的用户信息分页列表")
    public R<Page<QueryToBeAllocatedUserInfoVo>> getRoleToBeAllocatedUserInfoPage(@RequestBody @Validated QueryToBeAllocatedUserInfoDto queryDto) {
        return R.data(this.roleService.getRoleToBeAllocatedUserInfoPage(queryDto));
    }


    /**
     * 绑定角色用户关联
     *
     * @param bindRoleUsersDto
     * @return
     */
    @SaCheckPermission("system:role:bindRoleUsers")
    @PostMapping("/bindRoleUsers")
    @ApiOperation(value = "绑定角色用户关联")
    public R<Boolean> bindRoleUsers(@RequestBody @Validated BindRoleUsersDto bindRoleUsersDto) {
        this.userRoleService.bindRoleUsers(bindRoleUsersDto);

        // 判断当前操作用户是否是登录用户，更新用户权限
        Long currentUserId = SecurityUtils.getUserId();
        if (CollUtil.isNotEmpty(bindRoleUsersDto.getUserIds()) && bindRoleUsersDto.getUserIds().contains(currentUserId)) {
            AuthUtil.refreshUserPermission(currentUserId);
        }
        return R.status(true);
    }


    /**
     * 解除角色用户关联
     *
     * @param unBindRoleUsersDto
     * @return
     */
    @SaCheckPermission("system:role:unBindRoleUsers")
    @PostMapping("/unBindRoleUsers")
    @ApiOperation(value = "解除用户角色关联")
    public R<Boolean> unBindRoleUsers(@RequestBody @Validated BindRoleUsersDto unBindRoleUsersDto) {
        this.userRoleService.unBindRoleUsers(unBindRoleUsersDto);

        // 判断当前操作用户是否是登录用户，更新用户权限
        Long currentUserId = SecurityUtils.getUserId();
        if (CollUtil.isNotEmpty(unBindRoleUsersDto.getUserIds()) && unBindRoleUsersDto.getUserIds().contains(currentUserId)) {
            AuthUtil.refreshUserPermission(currentUserId);
        }
        return R.status(true);
    }


    /**
     * 启用角色
     *
     * @param roleId
     * @return
     */
    @GetMapping("/enabled/{roleId}")
    @ApiOperation("启用角色")
    @SaCheckPermission("system:role:enabled")
    public R<Boolean> enabled(@PathVariable("roleId") Long roleId) {
        this.roleService.enabled(roleId);
        return R.status(true);
    }


    /**
     * 停用角色
     *
     * @param roleId
     * @return
     */
    @GetMapping("/disabled/{roleId}")
    @ApiOperation("停用角色")
    @SaCheckPermission("system:role:disabled")
    public R<Boolean> disabled(@PathVariable("roleId") Long roleId) {
        this.roleService.disabled(roleId);
        return R.status(true);
    }

}
